package org.x.usermod.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.x.commonmod.config.Result;
import org.x.usermod.dto.GetDeptUsersDTO;
import org.x.usermod.dto.ModifyDeptDTO;
import org.x.usermod.dto.CreateDeptDTO;
import org.x.usermod.entity.SysDeptEntity;
import org.x.usermod.service.DeptService;
import org.x.usermod.vo.DeptTreeVO;
import org.x.usermod.vo.GetDeptListVO;
import org.x.usermod.vo.GetDeptUsersVO;

import java.util.List;

@RestController
@RequestMapping("/api/dept")
public class DeptController {
    /**
     * 创建部门
     * @return
     */
    @Autowired
    private DeptService deptService;

    @PostMapping("/create")
    public Result createDept(@RequestBody CreateDeptDTO createDeptDTO){
      Long deptId = deptService.createDept(createDeptDTO);
      return Result.ok(deptId);
    }

    /**
     * 更新部门
     * @param modifyDeptDTO
     * @return
     */
    @PutMapping("/update")
    public Result modifyDept(@RequestBody ModifyDeptDTO modifyDeptDTO){
        deptService.modifyDept(modifyDeptDTO);
        return Result.ok("部门更新成功");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteDept(@PathVariable("id") long id){
        deptService.deleteDept(id);
        return Result.ok("部门删除成功");
    }

    /**
     * 分页查询部门列表
     * @param
     * @return
     */
    @GetMapping("/list")
    public Result getDeptList(@RequestParam("page") Integer page,
                              @RequestParam("size") Integer size,
                              @RequestParam("name") String name){
        GetDeptListVO result = deptService.list(page,size,name);
        return Result.ok("部门列表查询成功",result);
    }

    /**
     * 部门详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result deptDetail(@PathVariable("id") long id){
        SysDeptEntity sysDeptEntity = deptService.getDeptDetail(id);
        return Result.ok("查询成功",sysDeptEntity);
    }

    /**
     * 部门树
     * @return
     */
    @GetMapping("/tree")
    public Result getDeepTree(){
        List<DeptTreeVO> deptTreeVOS = deptService.getDeepTree();
        return Result.ok("部门树查询成功",deptTreeVOS);
    }


    /**
     * 部门用户列表
     * @param getDeptUsersDTO 查询参数
     * @return
     */
    @GetMapping("users")
    public Result getDeptUser(@RequestBody GetDeptUsersDTO getDeptUsersDTO){
        GetDeptUsersVO deptUserList = deptService.getDeptUserList(getDeptUsersDTO);
        return Result.ok("查询成功",deptUserList);
    }

}